package top.kaoshanji.p3ch13a;

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import java.security.MessageDigest;

/**
 * author kaoshanji
 * ^_^
 * create time 2025/2/8 18:22
 */
public class MessageDigestUsage {

    public static void main(String[] args) throws Exception {

        // 代码清单-p3ch13a-428页a：使用MD5算法计算摘要的示例
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digest = md.digest("Hello World".getBytes());

        // 代码清单-p3ch13a-428页b：计算消息验证码的示例
        KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD5");
        SecretKey key = keyGenerator.generateKey();
        Mac mac = Mac.getInstance("HmacMD5");
        mac.init(key);

        byte[] result = mac.doFinal("Hello World".getBytes());
    }

}
